package edu.cicese.alz.reports;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 9/07/12
 * Time: 09:29 PM
 */
public class BlockPreview extends JPanel {
	protected int width, height;

	protected static Color[] accColors;
	protected static int[][] accValues;

	protected static Color[] gpsColors;
	protected static int[][] gpsValues;

	protected static List<BlockColor> nfcValues;
	protected static Color colorNfcNoData;

	public BlockPreview() {
		width = 480;
		height = 270;
		setPreferredSize(new Dimension(width, height));
		setMaximumSize(getPreferredSize());

		setAlignmentX(Component.LEFT_ALIGNMENT);
		setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1),
				BorderFactory.createLineBorder(Color.WHITE, 4)));
	}


	protected void loadAccelerometerValues() {
		try {
			ObjectInputStream inputStream = new ObjectInputStream(
					getClass().getClassLoader().getResourceAsStream("misc/intensity_block_preview.ct"));
			Object obj = inputStream.readObject();

			accValues = (int[][]) obj;
		} catch (IOException | ClassNotFoundException | NullPointerException e) {
			e.printStackTrace();
		}
	}

	protected void setAccelerometerColors(Color[] colors) {
		accColors = colors;
	}


	protected void setGPSValues() {
		int[] aux = new int[] {0, 0, 1, 1, 1, 1, 1, 2, 2, 2};
		int[] aux2 = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
		Random random = new Random();
		int rows = height / 30;
		for (int y = 0; y < rows; y++) {
			int randomBegin = random.nextInt(8);
			int randomEnd = random.nextInt(8);
			for (int x = 0; x < width / 5; x++) {
				if (x >= 20 + randomBegin && x <= 80 + randomEnd) {
					int randomInt = random.nextInt(10);
					gpsValues[y][x] = aux[randomInt];
				}
				else {
					int randomInt = random.nextInt(14);
					gpsValues[y][x] = aux2[randomInt];
				}
			}
		}
	}

	protected void setGPSColors(Color[] colors) {
		gpsColors = colors;
	}


	protected void setNFCValues(Color colorNoData, List<NFCTag> nfcTags) {
		colorNfcNoData = colorNoData;

		nfcValues = new ArrayList<>();
		Random random = new Random();
		for (int i = 0; i < nfcTags.size(); i++) {
			int randomInt = random.nextInt(10);
			for (int c = 0; c < randomInt; c++) {
				int randomX = random.nextInt(width);
				int randomY = random.nextInt(height / 30);
				nfcValues.add(new BlockColor(randomX, randomY, nfcTags.get(i)));
			}
		}
	}

	protected void updateNFCValues(Color colorNoData) {
		colorNfcNoData = colorNoData;
	}
}
